package com.lmf.ssm.services;

import com.lmf.ssm.bean.Employee;
import com.lmf.ssm.bean.EmployeeExample;
import com.lmf.ssm.dao.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    //查询所有员工
    public List<Employee> getAll(){
        List<Employee> list = employeeMapper.selectByExampleWithDept(null);
        return list;
    }

    //员工保存
    public void saveEmp(Employee employee) {
        //employee对象中，有效数据有几条，就插入几条
        int insert = employeeMapper.insertSelective(employee);
    }

    //检验用户名是否可用
    public Boolean checkusers(String empName) {
        //创建员工条件对象，逆向工程生成的
        EmployeeExample example=new EmployeeExample();
        //利用员工条件对象创建标准对象
        EmployeeExample.Criteria criteria = example.createCriteria();
        //利用标准对象添加各种条件
        criteria.andEmpNameEqualTo(empName);
        //调用mapper对象，按照条件查询方法
        long count = employeeMapper.countByExample(example);
        return count == 0 ;
    }

    //回显员工信息,按照员工id查询
    public Employee getEmp(Integer id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        return employee;
    }

    //保存修改的员工信息
    public void updateEmp(Employee employee) {
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    //按照id删除员工
    public void deleteEmpById(Integer id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    //批量删除员工
    public void deleteBatch(List<Integer> ids) {
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        //添加条件：根据用户id批量删除
        criteria.andEmpIdIn(ids);
        employeeMapper.deleteByExample(example);
    }
}
